package com.dd.stack.mybatisplus.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dd.stack.mybatisplus.pojo.BO.OrderDetailDTO;
import com.dd.stack.mybatisplus.pojo.BO.OrderStatsDTO;
import com.dd.stack.mybatisplus.pojo.BO.OrderWithUserDTO;
import com.dd.stack.mybatisplus.pojo.req.OrderQueryReq;
import com.dd.stack.mybatisplus.service.inner.impl.multi_table.OrderService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Author liuxianmeng
 * @CreateTime 2025/2/25 0:45
 * @Description Order多表操作（包括分页）
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/DO")
@Api(tags = "Order多表操作（包括分页）")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @GetMapping("/{orderId}")
    @ApiOperation("查询订单详情及用户信息")
    public ResponseEntity<OrderDetailDTO> getOrderDetailsWithUserInfo(@PathVariable Long orderId) {
        OrderDetailDTO orderDetail = orderService.getOrderDetailsWithUserInfo(orderId);
        return ResponseEntity.ok(orderDetail);
    }

    @PostMapping("/search/list")
    @ApiOperation("查询订单及其用户信息列表")
    public ResponseEntity<List<OrderWithUserDTO>> getOrdersWithUsers(@RequestBody OrderQueryReq req) {
        List<OrderWithUserDTO> orders = orderService.getOrdersWithUsers(req);
        return ResponseEntity.ok(orders);
    }

    @GetMapping("/stats")
    @ApiOperation("查询每个用户的订单统计信息")
    public ResponseEntity<List<OrderStatsDTO>> getOrderStatsByUser() {
        List<OrderStatsDTO> stats = orderService.getOrderStatsByUser();
        return ResponseEntity.ok(stats);
    }

    @PostMapping("/search/page")
    @ApiOperation("查询订单及其用户信息分页")
    public ResponseEntity<IPage<OrderWithUserDTO>> getOrdersWithUsersByPage(@RequestBody OrderQueryReq req) {
        IPage<OrderWithUserDTO> pageResult = orderService.getOrdersWithUsersByPage(req);
        return ResponseEntity.ok(pageResult);
    }
}
